package main

import (
	"fmt"
	"sync"
	"time"
)

/**
  读写锁
*/

type Total struct {
	value int
	mutex sync.RWMutex
}

var wg sync.WaitGroup

var total Total


func read() {
	defer wg.Done()
	//使用读锁
	total.mutex.RLock()
	time.Sleep(1000)
	fmt.Println()
	//释放读锁
	total.mutex.RUnlock()
}

func write() {
	defer wg.Done()
	//使用写锁
	total.mutex.Lock()
	total.value++
	time.Sleep(10 * time.Millisecond)
	//释放写锁
	total.mutex.Unlock()
}
